package com.example.controller;

import com.example.common.Result;
import com.example.entity.Im;
import com.example.service.ImService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/im")
public class ImController {

    @Resource
    private ImService imService;

    @PostMapping("/save")
    public Result save(@RequestBody Im im) {
        imService.save(im);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        imService.deleteById(id);
        return Result.success("删除成功");
    }

    @GetMapping("/init/{limit}")
    public Result selectAllInit(@PathVariable Integer limit) {
        List<Im> ims = imService.selectAllInit(limit);
        //new QueryWrapper<Im>().orderByDesc("id").last("limit " + limit)
//        return Result.success(ims.stream().sorted(Comparator.comparing(Im::getId)).collect(Collectors.toList()));
        return Result.success("初始化成功",ims);
    }

    @GetMapping("/selectAll")
    public Result selectAll(Im im){
        List<Im> list=imService.selectAll(im);
        return Result.success("查询聊天记录成功",list);
    }
}
